// pages/friends/createAccount/index.ts
import tran from '../../../utils/language';
import { commonRequestList } from '../../../utils/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    _t: tran._t('findPartner'),
    _tBase: tran._t('base'),
    type: 1,
    pickerParams: {
      show: false,
      pickerType: 'MBTI',
      defaultValue: ''
    },
    region: {},
    formData: {
      interestIds: '',
      professionId: '',
      personalities: '',
      professionName: '全部',
      languages: '',
      genderText: '',
      ageText: '',
      ageValue: '',
      minAge: 0,
      maxAge: 0,
      online: 'all',
      currentProvinceCode: '',
      currentCityCode: '',
      currentDistrictCode: '',
      requestPlayText: '',
      requestPlayIds: '',
      friend: false,
      languageText: '',
      gender: ''
    },
    partnerTypeList: ['在线游戏', '虚拟读书俱乐部', '电影之夜', '密室逃脱', '欢乐时光'],
    characterTypeList: ['ENTP', 'INTJ', 'ENTJ', 'ENFP', 'INFP'],
    multiIndex: [0, 0],
    commonPickerType: '',
    isEdit: getApp().globalData.profileFrom === 'edit',
    filterData: [{ text: '全部', key: '321231', selectFlag: true }, { text: '仅在线', key: '321431', selectFlag: false }],
    relationshipFilterData: [{ text: '全部', key: '1', selectFlag: true }, { text: '仅好友', key: '2', selectFlag: false }, { text: '新添加', key: '3', selectFlag: false }],
    additionalFlag: false,
    commonPickerGenderParams: {
      show: false,
      pickerType: 'gender',
      defaultValue: '',
      data: [] as commonPickerDataType[]
    },
    commonPickerAgeParams: {
      show: false,
      pickerType: 'age',
      defaultValue: '',
      data: [] as commonPickerDataType[]
    },
    pickerParams2: {
      show: false,
      pickerType: 'skill',
      defaultValue: '',
      title: '职业'
    },
    commonPickerParams: {
      show: false,
      pickerType: 'MBTI',
      defaultValue: '',
      data: [] as commonPickerDataType[]
    },
    playPickerParams: {
      show: false,
      pickerType: 'play',
      defaultValue: '',
      data: [] as commonPickerDataType[]
    },
    mbtiPickerParams: {
      show: false,
      pickerType: 'mbti',
      defaultValue: '',
      data: [] as commonPickerDataType[]
    },
    commonPickerData: [] as commonPickerDataType[],
    commonPickerGenderData: [] as commonPickerDataType[],
    langList: [],
    ageList: [
      {
        selectFlag: false,
        text: '18及以下',
        value: '0-18'
      },
      {
        selectFlag: false,
        text: '18-25',
        value: '18-25'
      },
      {
        selectFlag: false,
        text: '26-32',
        value: '26-32'
      },
      {
        selectFlag: false,
        text: '33-41',
        value: '33-41'
      },
      {
        selectFlag: false,
        text: '42-55',
        value: '42-55'
      },
      {
        selectFlag: false,
        text: '55及以上',
        value: '55-200'
      }
    ],
    playList: [],
    mbtiList: [],
    pickerHomeAreaParams: {
      show: false,
      pickerType: 'areaHomeRegion',
      defaultValue: [''],
      data: [] as commonPickerDataType[]
    },
    homeRegion: [''],
    regionTextList: []
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.getLangList()
    this.getProfession()
    this.getPlayList()
    this.getMbtil()
  },
  searchFn() {
    const { formData, regionTextList } = this.data
    const info = JSON.parse(JSON.stringify(formData))
    info.regionTextList = regionTextList
    const infoStr = JSON.stringify(info)
    if (!formData.genderText) {
      wx.showToast({
        title: '请选择性别',
        icon: 'none'
      })
      return false
    }
    if (!formData.requestPlayIds) {
      wx.showToast({
        title: '请选择要找什么搭子',
        icon: 'none'
      })
      return false
    }
    console.log(formData, '这里');
    wx.navigateTo({
      url: `/pages/profile/friendResult/index?info=${infoStr}&type=1`
    })
  },
  pickerHomeRegionCancle() {
    const { pickerHomeAreaParams } = this.data
    pickerHomeAreaParams.show = false
    this.setData({
      pickerHomeAreaParams
    })
  },
  /**
   * 区域选择器确认按钮
   * @param e 
   */
  commonHomeAreaPickerConfirm(e: any) {
    const obj = e.detail.value
    if (obj && Object.keys(obj).length > 0) {
      const { formData } = this.data
      const regionCodeList: any = [obj.provincesValue, obj.cityValue, obj.regionValue]
      const regionTextList: any = [obj.provincesLabel, obj.cityLabel, obj.regionLabel]
      if (regionCodeList.length > 0) {
        formData.currentProvinceCode = obj.provincesValue
        formData.currentCityCode = obj.cityValue
        formData.currentDistrictCode = obj.regionValue
      }
      this.setData({
        regionTextList,
        regionCodeList,
        formData,
      })
    }
  },
  handleCommonHomeAreaPicker(e: baseEvent) {
    const { pickerHomeAreaParams, formData, homeRegion } = this.data
    const type = e.currentTarget.dataset.type;
    pickerHomeAreaParams.pickerType = type
    pickerHomeAreaParams.show = true
    pickerHomeAreaParams.defaultValue = homeRegion
    this.setData({ pickerHomeAreaParams, commonPickerType: type })
  },
  getMbtil() {
    let list = getApp().globalData.mbtiList
    let mbtiList: any = []
    list.length > 0 && list.map((item: any) => {
      const obj = {
        text: item,
        value: item,
        selectFlag: false
      }
      mbtiList.push(obj)
    })
    this.setData({
      mbtiList
    })
  },
  /**
   * 获取一起玩列表
   */
  getPlayList() {
    const params = {
      requestType: 'PLAY'
    }
    commonRequestList(params).then(res => {
      res.length > 0 && res.map((item: any) => {
        item.text = item.reqName
        item.value = item.reqTypeId
      })
      this.setData({
        playList: res
      })
    })
  },
  /**
   * 从app.ts里面获取全局的语言列表
   */
  getLangList() {
    let list = getApp().globalData.langList
    let langList: any = [
      {
        selectFlag: false,
        text: '全部',
        value: 'all'
      }
    ]
    list.length > 0 && list.map((item: any) => {
      const obj = {
        selectFlag: false,
        ...item
      }
      langList.push(obj)
    })
    this.setData({
      langList
    })
  },
  additionalTap() {
    this.setData({
      additionalFlag: !this.data.additionalFlag
    })
  },
  toFriendSelectPage(e: any) {
    const currentIndex = e.target.dataset.index
    const { relationshipFilterData, formData } = this.data
    const flag = false;
    relationshipFilterData.length > 0 && relationshipFilterData.map((item, index) => {
      if (index === currentIndex) {
        item.selectFlag = true
        if (currentIndex == 1) {
          formData.friend = true
        } else if (currentIndex == 2) {
          formData.friend = false
        } else {
          formData.friend = null
        }
      } else {
        item.selectFlag = false
      }

    })
    this.setData({
      relationshipFilterData,
      formData
    })
  },
  toSelectPage(e: any) {
    const currentIndex = e.target.dataset.index
    const { filterData, formData } = this.data
    filterData.length > 0 && filterData.map((item, index) => {
      if (index === currentIndex) {
        item.selectFlag = true
      } else {
        item.selectFlag = false
      }
    })
    this.setData({
      filterData
    })
  },
  pickerCancel(e: { detail: any }) {
    if (e.detail) {
      // 有值表示是确认
      this.setData({ formData: { ...this.data.formData, ...e.detail } })
    }
    this.setData({ pickerParams: { show: false, pickerType: '', defaultValue: '' } })
  },
  bindMultiPickerColumnChange(e: any,) {
    let columnIndex = e.detail.column
    let valueIndex = e.detail.value
    let { multiIndex } = this.data
    if (columnIndex == 0) {
      multiIndex[0] = valueIndex
    } else {
      multiIndex[1] = valueIndex
    }
    this.setData({
      multiIndex
    })
  },
  handleCommonPicker(e: baseEvent) {
    const { commonPickerParams, langList } = this.data
    const type = e.currentTarget.dataset.type;
    commonPickerParams.pickerType = type
    commonPickerParams.show = true
    this.setData({ commonPickerParams, commonPickerData: langList, commonPickerType: type })
  },
  handlePlayPicker(e: baseEvent) {
    const { playPickerParams, playList } = this.data
    const type = e.currentTarget.dataset.type || 'play';
    playPickerParams.pickerType = type
    playPickerParams.show = true
    this.setData({ playPickerParams, commonPickerData: playList, commonPickerType: type })
  },
  handleMbtiPicker(e: baseEvent) {
    const { mbtiPickerParams, mbtiList } = this.data
    const type = e.currentTarget.dataset.type || 'mbti';
    mbtiPickerParams.pickerType = type
    mbtiPickerParams.show = true
    this.setData({ mbtiPickerParams, commonPickerData: mbtiList, commonPickerType: type })
  },
  /**
    * 获取职业技能列表
    */
  getProfession() {
    const listAll = wx.getStorageSync('professionList')
    let list = [{
      text: '全部',
      value: null,
      selectFlag: false,
      tagTypeId: ''
    }]
    listAll.length > 0 && listAll.map((item: any) => {
      item.text = item.tagName
      item.selectFlag = false
      item.value = item.tagTypeId
      list.push(item)
    })
    this.setData({
      selectList: list,
      professionList: list
    })
  },
  characterTypeChange(e: any) {
    const { formData, characterTypeList } = this.data
    const index = e.detail.value
    formData.characterType = characterTypeList[index]
    this.setData({
      formData
    })
  },
  bindRegionChange(e: any) {
    const { formData } = this.data
    const regionCodeList = e.detail.code
    const regionTextList = e.detail.value
    const regionPostcode = e.detail.opstcode
    if (regionCodeList.length > 0) {
      formData.currentProvinceCode = regionCodeList[0]
      formData.currentCityCode = regionCodeList[1]
      formData.currentDistrictCode = regionCodeList[2]
    }
    this.setData({
      regionTextList,
      regionCodeList,
      regionPostcode
    })
  },
  handleCommonSkillPicker(e: baseEvent) {
    const { pickerParams2 } = this.data
    const type = e.currentTarget.dataset.type;
    pickerParams2.pickerType = type
    pickerParams2.show = true
    this.setData({ pickerParams2, commonPickerType: type })
  },
  handleCommonAgePicker(e: baseEvent) {
    const { commonPickerAgeParams } = this.data
    // 打开picker 设置对应的picker弹出类型
    const type = e.currentTarget.dataset.type;
    commonPickerAgeParams.pickerType = type
    commonPickerAgeParams.show = true
    this.setData({ commonPickerAgeParams, commonPickerType: type })
  },
  handleCommonGenderPicker(e: baseEvent) {
    console.log(e,'222222');
    
    const { commonPickerGenderParams } = this.data
    let list = [
      {
        text: '全部',
        value: '',
        selectFlag: false
      },
      {
        text: '女性',
        value: '2',
        selectFlag: false
      },
      {
        text: '男性',
        value: '1',
        selectFlag: false
      },
      {
        text: '其他',
        value: '0',
        selectFlag: false
      }
    ]
    // 打开picker 设置对应的picker弹出类型
    const type = e.currentTarget.dataset.type || 'gender';
    commonPickerGenderParams.pickerType = type || 'gender'
    commonPickerGenderParams.show = true
    // commonPickerParams.data = langList
    this.setData({ commonPickerGenderParams, commonPickerGenderData: list, commonPickerType: type })
  },
  /**
   * 普通的选择器确认按钮
   * @param e 
   */
  commonPickerConfirm(e: any) {
    const { commonPickerType } = this.data
    const obj = e.detail.value
    const { formData } = this.data
    if (commonPickerType == 'language') { // 语言
      formData.languageText = obj.text
      formData.languages = obj.value
    } else if (commonPickerType == 'gender') { // 性别
      formData.genderText = obj.text
      formData.gender = obj.value
    } else if (commonPickerType == 'age') { // 年龄范围
      formData.ageText = obj.text
      const list = obj.value.split('-')
      formData.minAge = list[0]
      formData.maxAge = list[1]
      formData.ageValue = `${list[0]}-${list[1]}`
    } else if (commonPickerType == 'skill') { // 技能
      formData.professionName = obj.text
      formData.professionId = obj.value
    } else if (commonPickerType == 'play') { // 要找什么搭子
      formData.requestPlayText = obj.text
      formData.requestPlayIds = obj.value
    } else if (commonPickerType == 'mbti') { // 性格类型
      formData.personalities = obj.value
    }
    this.setData({
      formData
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})